<?php

namespace App\Http\Controllers\Admin;

use App\Models\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Houses;
use App\Models\Survey;

class HousesController extends Controller
{
    //房屋列表
    public function housesIndex(Request $request)
    {
        $where = function ($query) use ($request) {
            //项目名称
            if ($request->has('project_id') and $request->project_id != '') {

                $query->where('project_id', $request->project_id);
            }
            //房屋编号
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $search = "%" . $request->levy_house_num . "%";
                $query->where('levy_house_num', 'like', $search);
            }
            //产权人
            if ($request->has('owner') and $request->owner != '') {
                $search = "%" . $request->owner . "%";
                $query->where('owner', 'like', $search);
            }

            //手机号
            if ($request->has('phone') and $request->phone != '') {
                $search = "%" . $request->phone . "%";
                $query->where('phone', 'like', $search);
            }


        };
        $allProject=Project::all();
        $allHouses=Houses::where($where)->orderBy('created_at', 'desc')->paginate(10);
        return view('admin.houses.housesIndex',compact('allHouses','allProject'));
    }

    /*
     * 新建房屋
     * */
    public function newHouses(Request $request){
        $allProject=Project::all();
        return view('admin.houses.newHouses',compact('allProject'));
    }
    /*
     * 保存房屋信息
     * */
    public function saveHouses(Request $request){
        return  Houses::newHouses($request);
    }

    /*
    * 编辑房屋信息
    * */
    public function editHouses(Request $request){
        $allProject=Project::all();
        $arr=explode('=',$request->housesId);
        $housesId=$arr['1'];
        $editHouses=Houses::where('id',$housesId)->first();
        return view('admin.houses.editHouses',compact('editHouses','allProject'));
    }

    /*
   * 修改房屋信息
   * */
    public function updateHouses(Request $request){
        return  Houses::updateHouses($request);
    }

    /*
 * 删除房屋信息
 * */
    public function deleteHouses(Request $request){
        $result=Houses::destroy($request->housesId);
        if($result){
            $backMsg=['msg'=>200];
            return json_encode($backMsg);
        }
    }

    /*
     * 发送至签订合同
     * */
    public function sendToContract(Request $request){
        return Houses::sendToContract($request);
    }


    //调查明细列表
    public function housesDetail(Request $request)
    {
        $where = function ($query) use ($request) {
            //房屋编号
            if ($request->has('levy_house_num') and $request->levy_house_num != '') {
                $search = "%" . $request->levy_house_num . "%";
                $query->where('levy_house_num', 'like', $search);
            }
            //产权人
            if ($request->has('owner') and $request->owner != '') {
                $search = "%" . $request->owner . "%";
                $query->where('owner', 'like', $search);
            }
            /*//区块名称
            if ($request->has('survey_id') and $request->survey_id != '') {
                $search = "%" . $request->survey_id . "%";
                $query->where('survey_id', 'like', $search);
            }*/
            //手机号
            if ($request->has('phone') and $request->phone != '') {
                $search = "%" . $request->phone . "%";
                $query->where('phone', 'like', $search);
            }


        };
        $allHouses=Houses::where($where)->orderBy('created_at', 'desc')->paginate(10);
        return view('admin.houses.housesDetail',compact('allHouses'));
    }

}
